<script type="text/javascript">
$(document).ready(function(){
  if($("#LinkCategoryId").val()>0){
    $('#cat-selection div ul li #cat_id-'+$("#LinkCategoryId").val()).addClass('selected');
  }
  
  $("#cat-selection div ul li a").click(function(){ 
    $("#cat-selection div ul li a").removeClass('selected');
    var temp = new Array();
    temp = $(this).addClass('selected').attr('id').split('-');
    $("#LinkCategoryId").val(temp[1]);
    return false;
  })
    
  var options = {
	'maxCharacterSize': 500,
	'textFontSize': '13px',
	'textColor': '#5C2306',
	'textFamily': 'Tahoma,sans-serif',
	'textAlign': 'right',
	'warningColor': '#CC3300',  
	'warningNumber': 40,
	'isCharacterCount': true,
	'isWordCount': false,
	'charSpan': ''
  };
  $('#LinkContent').textareaCount(options);
})
</script>

<?php echo $this->Html->script('jquery.textareaCounter.plugin',false); ?>

<div class="links form">
<?php 

if($paso==2){
	
	echo $this->Form->create('Link');
?><fieldset>
	<legend><?php __('Link information'); ?></legend>
        <?php 
        if($this->data['Link']['type']=='image'){
        	echo($this->Html->image($this->data['Link']['url'],array('max-width'=>'100%','width'=>'100px')));
        }else{ ?>
        	<p class="genericformtxt">
          	  <b><?=$this->data['Link']['link_title'] ?></b><br />
          	  <?=$this->data['Link']['link_content'] ?><br />
          	  <b><?=$this->data['Link']['url'] ?></b>
        	</p>
        <?php 
        }
        
        if($warning) echo '<p class="error">'.$warning.'</p>';
        ?> 
    </fieldset>
    <fieldset>
	<legend><?php __('Links details'); ?></legend>

	<?php 
	    
		echo $this->Form->input('title');
		echo $this->Form->input('tags');
		echo $this->Form->input('content');
		echo $this->Form->input('paso',array('type'=>'hidden','value'=>2));
		echo $this->Form->input('hash',array('type'=>'hidden'));
		echo $this->Form->input('obj',array('type'=>'hidden'));
		
	?>
    </fieldset>
    
    
    <? if(count($this->data['Link']['link_images'])>0){ ?>
    <fieldset>
	<legend><?php __('Select a thumbnail'); ?></legend>
	<?php 
	
	$options=array(''=>$html->image('no-photo.jpg').'<br />Sin imagen');
	foreach ($this->data['Link']['link_images'] as $img){
		$options[md5($img)]=$html->image('link/tmp/'.md5($img).'.jpg');
	}
		
	$attributes=array('escape'=>false,'legend'=>false,'default'=>'text','default'=>md5($this->data['Link']['link_images'][0]));
	echo '<div id="imageRadio">'.$form->radio('thumb',$options,$attributes).'</div>' ?>
	
	</fieldset>
	<? } ?>
	
	<fieldset>
	<legend><?php __('Select a category'); ?></legend>
    	
	<?php 
	if(isset($this->validationErrors['Link']['category_id'])){
		echo '<div class="error-message">'.$this->validationErrors['Link']['category_id'].'</div>';
	}	
	?>
	<div id="cat-selection">
	<?php 
	$CATS=ClassRegistry::init('Category')->getCats();
	foreach ($CATS as $cat){
	?>
	  <div>
	    <h4><?=$cat['Category']['name'] ?></h4>
	    <ul>
	    <?php foreach ($cat['Children'] as $subcat){ ?>
	      <li><a id="cat_id-<?=$subcat['id'] ?>"><?=$subcat['name'] ?></a></li>
	    <? } ?>
	    </ul>
	    <div style="clear:both;border:none;margin:0;padding:0;"></div>
	  </div>
	<?php 
	}
	echo $this->Form->input('category_id',array('type'=>'hidden'));
	?>
	</div>

	</fieldset>
	<?php 
	echo $this->Form->input('url',array('type'=>'hidden'));
	echo $this->Form->input('blog_id',array('type'=>'hidden'));
	
	echo $this->Form->end(__('Submit', true));		
}else{
	echo $this->Form->create('Link');?>
	<fieldset>
 		<legend><?php __('Add Link'); ?></legend>
	<?php
	
		echo $this->Form->input('url');
	
		$options=array('text'=>$html->image('icon/news.png').'<br />Noticia','image'=>$html->image('icon/image.png').'<br />Imagen','video'=>$html->image('icon/video.png').'<br />Video');
	$attributes=array('escape'=>false,'legend'=>false,'default'=>'text');
	?><span style="font-size:120%;color:#EE3322;">Tipo:</span>
	 
	<div id="radioType"><?=$form->radio('type',$options,$attributes) ?></div>
		
	
	</fieldset>
	
<?php
	echo $this->Form->end(__('Submit', true));
}
?>
</div>
<div class="actions">
	<h3><?php __('Actions'); ?></h3>
	<ul>

		<li><?php echo $this->Html->link(__('List Links', true), array('action' => 'index'));?></li>
		<li><?php echo $this->Html->link(__('List Users', true), array('controller' => 'users', 'action' => 'index')); ?> </li>
		<li><?php echo $this->Html->link(__('New User', true), array('controller' => 'users', 'action' => 'add')); ?> </li>
		<li><?php echo $this->Html->link(__('List Blogs', true), array('controller' => 'blogs', 'action' => 'index')); ?> </li>
		<li><?php echo $this->Html->link(__('New Blog', true), array('controller' => 'blogs', 'action' => 'add')); ?> </li>
		<li><?php echo $this->Html->link(__('List Categories', true), array('controller' => 'categories', 'action' => 'index')); ?> </li>
		<li><?php echo $this->Html->link(__('New Category', true), array('controller' => 'categories', 'action' => 'add')); ?> </li>
		<li><?php echo $this->Html->link(__('List Comments', true), array('controller' => 'comments', 'action' => 'index')); ?> </li>
		<li><?php echo $this->Html->link(__('New Comment', true), array('controller' => 'comments', 'action' => 'add')); ?> </li>
		<li><?php echo $this->Html->link(__('List Favorites', true), array('controller' => 'favorites', 'action' => 'index')); ?> </li>
		<li><?php echo $this->Html->link(__('New Favorite', true), array('controller' => 'favorites', 'action' => 'add')); ?> </li>
		<li><?php echo $this->Html->link(__('List Tags', true), array('controller' => 'tags', 'action' => 'index')); ?> </li>
		<li><?php echo $this->Html->link(__('New Tag', true), array('controller' => 'tags', 'action' => 'add')); ?> </li>
		<li><?php echo $this->Html->link(__('List Trackbacks', true), array('controller' => 'trackbacks', 'action' => 'index')); ?> </li>
		<li><?php echo $this->Html->link(__('New Trackback', true), array('controller' => 'trackbacks', 'action' => 'add')); ?> </li>
		<li><?php echo $this->Html->link(__('List Votes', true), array('controller' => 'votes', 'action' => 'index')); ?> </li>
		<li><?php echo $this->Html->link(__('New Vote', true), array('controller' => 'votes', 'action' => 'add')); ?> </li>
	</ul>
</div>